package com.world.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * 请求日志切面类
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2023/10/08 16:55
 */
@Aspect
@Component
public class LogAspectHandler {

    private static final Logger logger = LoggerFactory.getLogger(LogAspectHandler.class);

    @Pointcut("execution(* com.world.business.*.controller..*.*(..))")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void doBefore(JoinPoint joinPoint) {

        logger.info("====doBefore方法进入了====");

        // 获取签名
        Signature signature = joinPoint.getSignature();
        // 获取切入的包名
        String declaringTypeName = signature.getDeclaringTypeName();
        // 获取即将执行的方法名
        String funcName = signature.getName();
        logger.info("即将执行方法为: {}，属于{}包", funcName, declaringTypeName);

        // 也可以用来记录一些信息，比如获取请求的url和ip
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        // 获取请求url
        String url = request.getRequestURL().toString();
        // 获取请求ip
        String ip = request.getRemoteAddr();
        logger.info("用户请求的url为：{}，ip地址为：{}", url, ip);
    }

    @AfterReturning(value = "pointcut()", returning = "result")
    public void doAfterReturning(JoinPoint joinPoint, Object result) {

        logger.info("====doAfterReturning方法进入了====");

        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        logger.info("方法{}执行完成，返回参数为：{}", name, result);
        // 实际项目中可以根据业务做具体的返回值增强
        logger.info("对返回的参数进行业务上的增强：{}", result.toString() + "增强");

    }

    @After("pointcut()")
    public void doAfter(JoinPoint joinPoint) {

        logger.info("====doAfter方法进入了====");

        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        logger.info("方法{}已经执行完", name);
    }

    /**
     * 在上面定义的切面方法执行抛异常时，执行该方法
     *
     * @param joinPoint jointPoint
     * @param ex        ex
     */
    @AfterThrowing(pointcut = "pointcut()", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, Throwable ex) {
        Signature signature = joinPoint.getSignature();
        String method = signature.getName();
        // 处理异常的逻辑
        logger.info("执行方法{}出错，异常为：{}", method, ex);
    }
}
